/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.jr.rest.server;

import cn.ac.iie.di.ban.jr.rest.global.GlobalParas;
import cn.ac.iie.di.ban.jr.rest.handlers.Handler;
import cn.ac.iie.di.ban.jr.rest.handlers.dayhot.DayHotJsongHandler;
import cn.ac.iie.di.ban.jr.rest.handlers.dayhot.WeekHotJsongHandler;
import cn.ac.iie.di.ban.jr.rest.util.FileAppender;
import cn.ac.iie.di.ban.jr.rest.util.SenderPool;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import javax.xml.bind.annotation.XmlElementDecl;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author zwlin
 */
public class TXRestStartup {

    public static Logger LOG = Logger.getLogger(TXRestStartup.class);
    static Server server = null;

    public static void main(String[] args) {
        try {
            GlobalParas.getIstance(GlobalParas.DEFAULT_CONFIGURATION);
            LOG.info("Init Configuration");
        } catch (ConfigurationException e) {
            LOG.error(ExceptionUtils.getFullStackTrace(e));
        }

        try {
            server = new Server(GlobalParas.REST_SERVER_IP, GlobalParas.REST_SERVER_PORT, GlobalParas.REST_SERVER_PARALLEL_SIZE);
            LOG.info("Init Server");
            // wxyq_dayhot_$data.1
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
            String dateNowStr = sdf.format(date);
            FileAppender fileAppender = new FileAppender(GlobalParas.FILE_DIR, "wxyq_dayhot_" + dateNowStr, GlobalParas.FILE_SIZE, GlobalParas.FILE_TIMEOUT);
            fileAppender.start();
            Handler.regJsonHandler(new DayHotJsongHandler(GlobalParas.COLUMN_TYPE_MAP,
                    new SenderPool("", ""),
                    fileAppender));
            Handler.regJsonHandler(new WeekHotJsongHandler(GlobalParas.COLUMN_TYPE_MAP,
                    new SenderPool("", ""),
                    fileAppender));
            server.start();
            LOG.info("Start Server");
        } catch (Exception e) {
            try {
                server.stop();
            } catch (Exception e1) {
                LOG.error(ExceptionUtils.getFullStackTrace(e1));
            }
            LOG.error(ExceptionUtils.getFullStackTrace(e));
        }
    }
}
